package servlet;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Account;
import model.Connect;
import model.SQLModel;
import model.UserInfo;



/**
 * Servlet implementation class ServletLogin
 */
public class ServletUpdateUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletUpdateUser() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try{

			if (request.getMethod().equalsIgnoreCase("post")||request.getMethod().equalsIgnoreCase("get")) {
				try {
					Connect connect = new Connect();
					Account account=connect.checkSession(request, response);
					connect.setSID(connect.getdbSession(request));
					connect.setUser(account.strUserName);
					connect.setPassword(account.strPass);
					Connection conn = connect.simple_connect();
					SQLModel db = new SQLModel(conn);
					String user = account.strUserName;
					db.alterPassword(request.getParameter("user"), request.getParameter("pwd"));
					if(user.equalsIgnoreCase("admin")){
						db.alterOtherInfo(request.getParameter("user"), request.getParameter("status"), request.getParameter("quota"), request.getParameter("role"), request.getParameter("profile"));
					}
	//				System.out.println(request.getParameter("user")+ request.getParameter("status")+ request.getParameter("quota")+ request.getParameter("role")+ request.getParameter("profile"));
	//				db.alterOtherInfo(username, status, quota, role, profile)
					conn.close();
					if(!"admin_role".equalsIgnoreCase(account.strRole)){
						response.sendRedirect("./ServletLogout");
					}
					else {
						response.sendRedirect("./UserManagement");
					}
					
				} catch (Exception e) {
					System.out.print("Connection unsuccessfly "+ e.getMessage());
					//response.sendRedirect("ManageAccount/Login.jsp");
				}
			}

		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

}
